/* 35. 搜索插入位置 */
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var searchInsert = function (nums, target) {
	const n = nums.length
	let left = 0,
		right = n - 1,
		ans = n
	while (left <= right) {
		const middle = left + ((right - left) >> 1)
		if (target <= nums[middle]) {
			ans = middle
			right = middle - 1
		} else {
			left = middle + 1
		}
	}
	return ans
}
var searchInsert = function (nums, target) {
	if (target < nums[0]) {
		return 0
	} else if (target > nums[nums.length - 1]) {
		return nums.length
	} else {
		for (let i = 0; i < nums.length; i++) {
			if (nums[i] === target) {
				return i
			} else if (nums[i] < target && nums[i + 1] > target) {
				return i + 1
			}
		}
	}
}

const res = searchInsert([1, 3, 5, 6], 2)
console.log('searchInsert :>>', res)
